package cn.avalon.cotroller;

import cn.avalon.downgrade.DowngradeUtil;
import cn.avalon.domain.User;
import cn.avalon.downgrade.FuseDowngradeUtil;
import cn.avalon.feigenClients.UserFeignClient;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: Order接口
 * @author: Avalon
 * @date: 2022年09月22日 ： 10:50
 * @version: ver1.0
 * @email 709785494@qq.com
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    // 注入feign接口
    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/{id}")
    @SentinelResource(value = "order-service", blockHandler = "orderBlockHandler", blockHandlerClass = DowngradeUtil.class, fallback = "orderFallback", fallbackClass = FuseDowngradeUtil.class)
    public User findById(@PathVariable("id") Long id) {

        int a = 1 / 0;

        return userFeignClient.findById(id);
    }

//    public User orderBlockHandler(@PathVariable("id") Long id, BlockException e) {
//        e.printStackTrace();
//        return new User(-1L, "服务降级触发！", "当前服务流量过大，请稍后重试！");
//    }

//    public User orderFallback(@PathVariable("id") Long id, Throwable e) {
//        e.printStackTrace();
//        return new User(-1L, "熔断服务降级触发！", "服务不可用，请稍后重试！");
//    }
}
